<?php
	header("Content-type: text/html; charset=utf-8");
	//include('class/db.class.php');//导入数据库类
	include('class/downloads.class.php');
	include('class/upload.class.php');
	require('smartyconfig.php');

	$db = new DB();//实例化对象
	
	/* 查询数据 */
	$get_rt = $db->get_all('select * from test');
	//$arr = array(array(id=>'1',id2=>'2',id3=>'3'),array(aa=>'1',bb=>'2',cc=>'3'),array(qq=>'1',ww=>'2',ee=>'3'));
	//var_dump($get_rt);
	
	/* 修改数据 */
	$id = $_GET['edit'];
	if($id){$get_update = $db->get_one('select * from test where id ='.$id);}

	if($_POST['edit_sub']){
		$xueli = $_POST['xueli'];
		//$get_id = $db->get_one('select * from test where id ='.$id);
		//$ide = $get_id['id'];
		//var_dump($ide);exit;
		$update = $db->update('test',array(xueli=>"$xueli"),'id ='.$ide);
		echo "<script>alert('修改成功!');window.location.href='index.php'</script>"; 
	}
	
	/* 添加数据 */
	if($_POST['submit']){
		$name = $_POST['name'];
		$xueli = $_POST['xueli'];
		$age = $_POST['age'];
		
		/* 上传头像--开始 */
		if($_FILES['myfile']['name'] != ''){
			if($_FILES['myfile']['error'] > 0){
				switch($_FILES['myfile']['error']){
					case 1:
						echo "文件大小超过了PHP.ini中的文件限制！";
					break;
					case 2:
						echo "文件大小超过了浏览器限制！";
					break;
					case 3:
						echo "文件部分被上传！";
					break;
					case 4:
						echo "没有找到要上传的文件！";
					break;
					case 5:
						echo "服务器临时文件夹丢失，请重新上传！";
					break;
					case 6:
						echo "文件写入到临时文件夹出错！";
					break;
				}
			} else {
				if((($_FILES["myfile"]["type"] == "image/gif") or ($_FILES["myfile"]["type"] == "image/jpeg") or ($_FILES["myfile"]["type"] == "image/pjpeg") or ($_FILES["myfile"]["type"] == "image/png")) && ($_FILES["myfile"]["size"] < 1480000)) {
					if (!file_exists("uploads/")){						
						//echo "uploads/".date("m")."/".$_FILES["myfile"]["name"];exit;
						//move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/".$_FILES['myfile']['name']);
						/*echo "<script>alert(上传成功！);</script>";*/
						@mkdir("uploads/", 0777);
						@chmod("uploads/",0777);
						if(!file_exists("uploads/".date("m"))){
							@mkdir("uploads/".date("m"),0777);
							@chmod("uploads/".date("m"),0777);
							if(is_uploaded_file($_FILES["myfile"]["tmp_name"]))
							{
								move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/".date("m")."/".$_FILES['myfile']['name']);
							}
						}
					}
					else
					{
						if(!file_exists("uploads/".date("m"))){
							@mkdir("uploads/".date("m"),0777);
							@chmod("uploads/".date("m"),0777);
							if(!file_exists("uploads/".date("m")."/".$_FILES['myfile']['name'])){
								if(is_uploaded_file($_FILES["myfile"]["tmp_name"]))
								{
									move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/".date("m")."/".$_FILES['myfile']['name']);
								}
							}
						}else{
							if(!file_exists("uploads/".date("m")."/".$_FILES['myfile']['name'])){
								if(is_uploaded_file($_FILES["myfile"]["tmp_name"]))
								{
									move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/".date("m")."/".$_FILES['myfile']['name']);
								}
							}else{
								echo "<script>alert('您上传的头像已经存在！');window.location.href='index.php'</script>";
								return false;
							}
						}
					}
				}else{
					echo "<script>alert('图片格式不正确或者图片不能大于10M!');window.location.href='index.php'</script>";
					return false;
				}
			} 
		}else{
			echo "<script>alert('请选择要上传的头像!');window.location.href='index.php'</script>";
			return false;
		} 
		/* 上传头像--结束 */
		
		/* 上传文件--开始 */
		if($_FILES['filedoc']['name'] != ''){
		
			if($_FILES['filedoc']['error'] > 0){
				switch($_FILES['filedoc']['error']){
					case 1:
						echo "文件大小超过了PHP.ini中的文件限制！";
					break;
					case 2:
						echo "文件大小超过了浏览器限制！";
					break;
					case 3:
						echo "文件部分被上传！";
					break;
					case 4:
						echo "没有找到要上传的文件！";
					break;
					case 5:
						echo "服务器临时文件夹丢失，请重新上传！";
					break;
					case 6:
						echo "文件写入到临时文件夹出错！";
					break;
				}
			} else {
				if((($_FILES["filedoc"]["type"] == "application/msword") or ($_FILES["filedoc"]["type"] == "application/vnd.ms-excel") or ($_FILES["filedoc"]["type"] == "text/plain") or ($_FILES["filedoc"]["type"] == "text/html") or ($_FILES["filedoc"]["type"] == "application/octet-stream")) && ($_FILES["filedoc"]["size"] < 1480000)) {
					if (!file_exists("uploads/")){						
						@mkdir("uploads/", 0777);
						@chmod("uploads/",0777);
						if(!file_exists("uploads/".date("m")."_file")){
							@mkdir("uploads/".date("m")."_file",0777);
							@chmod("uploads/".date("m")."_file",0777);
							
							if(is_uploaded_file($_FILES["filedoc"]["tmp_name"]))
							{
								move_uploaded_file($_FILES['filedoc']['tmp_name'] , "uploads/".date("m")."_file/".$_FILES['filedoc']['name']);
							}
						}
					}
					else
					{
						if(!file_exists("uploads/".date("m")."_file")){
							@mkdir("uploads/".date("m")."_file", 0777);
							@chmod("uploads/".date("m")."_file",0777);
						}
						if(!file_exists("uploads/".date("m")."_file/".$_FILES['filedoc']['name'])){
							
							if(is_uploaded_file($_FILES["filedoc"]["tmp_name"]))
							{
								//echo $_FILES['filedoc']['name'];
								//move_uploaded_file($_FILES['filedoc']['tmp_name'],"uploads/".date("m")."_file/".$_FILES['filedoc']['name']);
								move_uploaded_file($_FILES['filedoc']['tmp_name'],"uploads/".date("m")."_file/".trim(iconv('utf-8','gb2312',trim($_FILES['filedoc']['name']))));
							}
						}else{
							echo "<script>alert('您上传的文件已经存在！');window.location.href='index.php'</script>";
							return false;
						}
					}
				}else{
					echo "<script>alert('文件格式不正确或者文件不能大于10M!');window.location.href='index.php'</script>";
					return false;
				}
			} 
		}else{
			echo "<script>alert('请选择要上传的文件!');window.location.href='index.php'</script>";
			return false;
		} 
		/* 上传文件--结束 */
		
		//$ran = rand(0,100);
		//var_dump($_FILES['filedoc']);exit;
		$path_doc = "uploads/".date("m")."_file/".$_FILES['filedoc']['name'];
		//var_dump($path_doc);exit;
		$doc_name = $_FILES['filedoc']['name'];

		$path_img = "uploads/".date("m")."/".$_FILES['myfile']['name'];
		$insert = $db->insert('test',array(name=>"$name",xueli=>"$xueli",age=>"$age",portrait=>"$path_img",file_doc=>"$path_doc",doc_name=>"$doc_name"));
    	echo "<script>alert('添加成功!');window.location.href='index.php'</script>";
	}
	
	/* 下载文件*/
	if(isset($_GET['act']) && $_GET['act'] == 'down'){
		$file_name=$_GET['FileName'];
		$dl = new Download();
		$dl->downloads($file_name);
	}

	/* 删除数据 */
	$ids = $_GET['del'];
	if($ids){
		$delete = $db->delete('test','id =' .$ids);
		echo "<script>alert('删除成功!');window.location.href='index.php'</script>";
	}
	
$smarty->assign("title",首页);
$smarty->assign("id",$id);
$smarty->assign("info",$get_rt);
$smarty->assign("info_update",$get_update);

$smarty->display('index.html');
?>